package com.gt.login;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListUserDetailArrayAdapter extends ArrayAdapter<User> {
	private List<User> usersL;
	private LayoutInflater inflator;

	public ListUserDetailArrayAdapter(Context context, int textViewResourceId, List<User> usersL) {
		super(context, textViewResourceId, usersL);
		this.usersL = usersL;
		inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		User usr = usersL.get(position);
		if (null == convertView) {
			// inflate xml layout
			convertView = inflator.inflate(R.layout.list_layout, viewGroup, false);
		}
		// get fields and set values
		TextView crDate = (TextView) convertView.findViewById(R.id.crDateTv);
		TextView crTime = (TextView) convertView.findViewById(R.id.crTimeTv);
		TextView firstName = (TextView) convertView.findViewById(R.id.firstNameTv);
		crDate.setText("Created Date " + getFormattedDate(usr.getCreatedDate()));
		crTime.setText("Created Time " + getFormattedTIme(usr.getCreatedTime()));
		firstName.setText(usr.getFirstName());
		return convertView;
	}

	private String getFormattedDate(String date) {
		SimpleDateFormat fromUser = new SimpleDateFormat("yyyMMdd");
		SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
		String reformattedStr = "";
		try {
			reformattedStr = myFormat.format(fromUser.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return reformattedStr;
	}

	private String getFormattedTIme(String time) {
		SimpleDateFormat fromUser2 = new SimpleDateFormat("HHmmss");
		SimpleDateFormat myFormat2 = new SimpleDateFormat("hh:mm a");
		String reformattedStr = "";
		try {
			reformattedStr = myFormat2.format(fromUser2.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return reformattedStr;
	}

}
